package com.offer;

import com.bean.TreeNode;
import com.learn.bean.Node;

/**
 * @Author: huangzhigao
 * @Date: 2023/3/4 18:48
 */
public class leecode36 {
    private TreeNode pre;
    private TreeNode head;

    public TreeNode treeToDoublyList(TreeNode root) {
        if(root == null){
            return null;
        }
        dfs(root);
        head.left = pre;
        pre.right = head;
        return head;

    }


    public void dfs(TreeNode cur) {
        if (cur == null) {
            return;
        }
        dfs(cur.left);
        if (pre == null) {
            head = cur;
        } else {
            pre.right = cur;
        }

        cur.left = pre;
        pre = cur;
        dfs(cur.right);
    }


}
